﻿Imports IICModel.BusObjects
Imports IICModel.BusObjects.Mappers
Imports IICBusinessLogic.PeriodProcessing
Imports IICModel.ConversionObjects.Mappers
Imports IICModel.ConversionObjects
Imports IICModel.DataConbversionLogic
Imports IICModel
Imports org.model.lib


Namespace DataConversion


    Public Class PASConversionUpdate


        Public Shared Sub main()
            Try

                Model.ModelContext.Current.CurrentUserName = BaseDataConverter.DATA_CONVERSION_USERNAME
                Model.ModelContext.CurrentDBUtils = BaseDataConverter.newDBConnection

                Dim cu As New PASConversionUpdate

                cu.pasDasStructureManager = New IICBusinessLogic.PasDasStructureManager

                cu.insertMissingPASPeriods()
                cu.reverseAllPASandDASPeriods()
                cu.pasDasStructureManager.updatePasDasStructure()
                cu.createDummyRecordsForOldMembers()

                Dim pers As List(Of Period) = PeriodDataUtils.findList("[DiplomaSuperTypeId] in (2,5) and dataConversionSourceTable ='EX_PERDS' order by perioddate")
                Call InitialConversion.ProcessPeriodList(pers)
                PeriodDataUtils.savePeriod(pers.ToArray)



            Finally
                ModelContext.release()
            End Try
        End Sub

        Private pasDasStructureManager As IICBusinessLogic.PasDasStructureManager

        Private period92 As Period
        Private period93 As Period

        Private credits As New List(Of StudentLessonCredit)

        Private Sub insertMissingPASPeriods()

            Me.period92 = PeriodDataUtils.findOne("PeriodCode='92A'")
            If Me.period92 Is Nothing Then

                Me.period92 = PeriodFactory.Create
                Me.period92.PeriodDate = DateSerial(1992, 1, 1)
                Me.period92.PeriodCode = "92A"
                Me.period92.DataConversionSourceTable = "EX_PERDS"
                Me.period92.DiplomaSuperTypeId = EnumDiplomaSuperTypes.PAS
                PeriodDataUtils.savePeriod(Me.period92)
            End If

            Me.period93 = PeriodDataUtils.findOne("PeriodCode='93A'")
            If Me.period93 Is Nothing Then
                Me.period93 = PeriodFactory.Create
                Me.period93.PeriodDate = DateSerial(1993, 5, 13)
                Me.period93.PeriodCode = "93A"
                Me.period93.DataConversionSourceTable = "EX_PERDS"
                Me.period93.DiplomaSuperTypeId = EnumDiplomaSuperTypes.PAS
                PeriodDataUtils.savePeriod(Me.period93)
            End If

        End Sub

        Private Sub reverseAllPASandDASPeriods()

            Dim pp As PeriodProcess = Nothing
            Dim pps As List(Of PeriodProcess) = PeriodProcessDataUtils.findList( _
                "ReversalDate is null and PeriodId in (select PeriodId from period where [DiplomaSuperTypeId] in (2,5) and dataConversionSourceTable ='EX_PERDS') order by PeriodProcessDate desc")

            For Each pp In pps

                Dim bclosePer As Boolean = False
                Dim period As Period = PeriodDataUtils.findByKey(pp.PeriodId.GetValueOrDefault)
                If (period.IsOpen = False) Then
                    period.IsOpen = True
                    bclosePer = True
                    PeriodDataUtils.savePeriod(period)
                End If

                Dim preverse As PeriodProcessReverser = New PeriodProcessReverser(period)
                Console.WriteLine("reversing " & period.PeriodDescription)
                preverse.reversePeriodProcess()

                'If bclosePer Then
                '    period.IsOpen = False
                '    PeriodDataUtils.savePeriod(period)
                'End If

            Next
        End Sub

        Private Sub createDummyRecordsForOldMembers()


            'select * from [STGIICDB].[dbo].[MEMBERS] where (PAS_1_DT is not null and PAS_1_DT < '1993-12-31')
            Dim lst As List(Of MEMBERS) = MEMBERSDataUtils.findList("(PAS_1_DT is not null and Year(PAS_1_DT)<=1993) or MEMBER_NO IN (366,512,929,862,513,980,972,844,1018,1091,1142,1226,804,36,76,129,424,568,1127,2281,3753,3757) ")

            For Each m As MEMBERS In lst

                'Dim loopMember As IMEMBERS = m ' thsi is here because the "m" variable cannot be used in the lanbda expression below
                Dim student As Student = StudentDataUtils.findOne(" MemberNumber={0}", m.MemberNo)
                If student Is Nothing Then
                    Throw New ApplicationException("student not found:" & m.MemberNo)
                End If
                Dim ldps As CertificationType = Me.pasDasStructureManager.dimplomaTypePasVASIKO.DiplomaRequirements(0).Certification
                For Each x As CertificationRequirement In ldps.CertificationRequirements
                    Call createLessonExcetion(x, student, m.Pas1Dt.GetValueOrDefault, m)
                Next

            Next

            lst = MEMBERSDataUtils.findList("(PAS_2_DT is not null and Year(PAS_2_DT)<=1993) ")
            'or (MEMBER_NO IN (296,2096,2669,3370,3415,4402,4406) and PAS_2_DT< '2009-06-01')

            For Each m As MEMBERS In lst

                Dim student As Student = StudentDataUtils.findOne(" MemberNumber={0}", m.MemberNo)
                If student Is Nothing Then
                    Throw New ApplicationException("student not found:" & m.MemberNo)
                End If
                Dim ldps As CertificationType = Me.pasDasStructureManager.dimplomaTypePasANOTERO.DiplomaRequirements(0).Certification
                For Each x As CertificationRequirement In ldps.CertificationRequirements
                    Call createLessonExcetion(x, student, m.Pas2Dt.GetValueOrDefault, m)
                Next

            Next

            lst = MEMBERSDataUtils.findList("(PAS_3_DT is not null and Year(PAS_3_DT)<=1993) ")
            'or MEMBER_NO in (296,862,1026,1657,1661,1669,1810,2068,2101,2669,2749,2676,2689,3302,3370,3544,3660,3798,4402,4406,4419)

            For Each m As MEMBERS In lst

                Dim student As Student = StudentDataUtils.findOne(" MemberNumber={0}", m.MemberNo)
                If student Is Nothing Then
                    Throw New ApplicationException("student not found:" & m.MemberNo)
                End If
                Dim ldps As CertificationType = Me.pasDasStructureManager.dimplomaTypePasANOTATO.DiplomaRequirements(0).Certification
                For Each x As CertificationRequirement In ldps.CertificationRequirements
                    Call createLessonExcetion(x, student, m.Pas3Dt.GetValueOrDefault, m)
                Next

            Next


            StudentLessonCreditDataUtils.saveStudentLessonCredit(credits.ToArray)

        End Sub

        Private Sub createLessonExcetion(ByVal x As CertificationRequirement, _
                                         ByVal student As Student, ByVal effdate As Date, _
                                         ByVal loopMember As IMEMBERS)

            Dim existingCredit As StudentLessonCredit = StudentLessonCreditDataUtils.findOne("studentid={0} and lessonid={1}", student.StudentId, x.LessonId)

            If existingCredit Is Nothing Then

                Dim s As StudentLessonCredit = StudentLessonCreditFactory.Create
                s.StudentId = student.StudentId
                s.LessonId = x.LessonId
                s.Comments = "Data Conversion of old PAS holders for periods 1992 and 1993"
                If Year(effdate) = 1992 Then
                    s.EffectiveDate = period92.PeriodDate
                ElseIf Year(effdate) = 1993 Then
                    s.EffectiveDate = period93.PeriodDate
                Else
                    s.EffectiveDate = effdate
                End If
                credits.Add(s)
                Console.WriteLine("***Credit created for " & student.MemberNumber & "," & x.Certification.CertificationType)

            Else
                Console.WriteLine("Credit already exists")
            End If

        End Sub

    End Class

End Namespace